package com.lzw.bluetoothpractice.Activitys;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.View;
import android.widget.Button;

import com.lzw.bluetoothpractice.Fragment.DetailFragment;
import com.lzw.bluetoothpractice.Fragment.EquipmentFragment;
import com.lzw.bluetoothpractice.R;

public class DetailMyActivity extends AppCompatActivity implements View.OnClickListener{


    Button bt_my,bt_equipment;//两个按钮
    Fragment equipmentfragment = new EquipmentFragment();
    Fragment detailfragment = new DetailFragment();
    Fragment fragments[];
    Button buttons[];
    int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_my);
        init();
        buttons[0].setOnClickListener(this);
        buttons[1].setOnClickListener(this);
    }
    //设置默认的Fragment
    private void setFragment(int currentIndex, int index) {
        if (currentIndex != index) {
            //在androidx支持的包中
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction ft = manager.beginTransaction();
            ft.replace(R.id.fl,fragments[currentIndex]);
            ft.commit();
            //直接创建FragmentTransaction对象即可，然后调用对象中的方法进行Fragment页面布局的加载
            buttons[currentIndex].setSelected(true);
            buttons[index].setSelected(false);
        }
    }
    //初始化
    private void init() {
        bt_my = (Button)this.findViewById(R.id.button_i);
        bt_equipment = (Button)this.findViewById(R.id.button_equipment);
        fragments = new Fragment[]{detailfragment,equipmentfragment};
        buttons = new Button[]{bt_my,bt_equipment};
        //设置默认fragment
        setFragment(0,1);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button_i:
                setFragment(0, index);
                index = 0;
                break;
            case R.id.button_equipment:
                setFragment(1, index);
                index = 1;
                break;
        }
    }
}
